package com.example.czy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.czy.annotation.SysLog;
import com.example.czy.api.feign.GoodsApi;
import com.example.czy.base.qo.PageQO;
import com.example.czy.qo.GoodsQO;
import com.example.czy.service.GoodsService;
import com.example.czy.utils.R;
import com.example.czy.vo.GoodsVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 商品控制器
 *
 * @author czy
 * @date 2023-07-09
 **/
@RestController
@RequiredArgsConstructor
@RequestMapping("/goods")
@Tag(description = "goods", name = "商品")
public class GoodsController implements GoodsApi {
    private final GoodsService goodsService;

    /**
     * 后端自定义条件查询示例
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page-example")
    @SysLog(name = "日志测试")
    public R<Page<GoodsVO>> pageExample(PageQO page) {
        return R.ok(goodsService.pageExample(page));
    }


    /**
     * 分页查询 根据QO（注解）
     *
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<Page<GoodsVO>> page(PageQO page, GoodsQO qo) {
        return R.ok(goodsService.pageByQO(page, qo));
    }

    /**
     * 分页查询 根据JSON 条件前端传输
     *
     * @param page
     * @param json [{"name":"lk 张三"}]
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page-json")
    public R<Page<GoodsVO>> pageByJson(PageQO page, String json) {
        return R.ok(goodsService.pageByJson(page, json));
    }


    @DeleteMapping
    @Operation(summary = "删除", description = "删除")
    public R<Boolean> deleteByIds(String ids) {
        return R.ok(goodsService.removeBatchByIds(Arrays.asList(ids.split(","))));
    }


}
